﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp5
{
    class 虚方法
    {
        public 虚方法()
        {
            Test test1 = new Test();
            Test test2 = new T1();
            T1 test3 = new T1();

            Console.WriteLine("测试对象里面的函数");
            test1.SetData(2);
            Console.WriteLine(test1.GetData());
            Console.WriteLine("T1对象");
            test2.SetData(2);
            Console.WriteLine(test2.GetData());
            Console.WriteLine("T1对象");
            test3.SetData(2);
            Console.WriteLine(test3.GetData());
        }
        class Test
        {
            internal int data;

            public virtual void SetData(int data)
            {
                this.data = data;
            }
            public int GetData()
            {
                return data;
            }
        }
        class T1 : Test
        {
            public override void SetData(int data)
            {
                this.data = data * data;
            }
            public int GetData()
            {
                return data * data;
            }
        }
    }
}
